import { _decorator, Component, Enum, math, Node, random, randomRange, tween, v2, v3, Vec2, Vec3, view, View } from 'cc';
const { ccclass, property } = _decorator;

enum RandomMoveEnum {
    /**
     * 事业范围内
     */
    View,
}
Enum(RandomMoveEnum)

/**
 * 随机移动
 */
@ccclass('RandomMove')
export class RandomMove extends Component {

    private _moveSpeed_n = 200
   
    protected start(): void {
        this.node.setWorldPosition(this.getRandomWorldPoint())
        this.move(true)
    }

    private getRandomWorldPoint(): Vec3 {
        const view_size = view.getVisibleSize();
        const randomX_n = randomRange(0, view_size.width);
        const randomY_n = randomRange(0, view_size.height);
        return v3(randomX_n, randomY_n)
    }
    
    private move(isLoop = false) {
        const startPoint_v3 = this.node.getWorldPosition()
        const endPoint_v3 = this.getRandomWorldPoint()
        const moveTime_n =  startPoint_v3.clone().subtract(endPoint_v3).length() / this._moveSpeed_n
        tween(this.node)
            .to(
                moveTime_n,
                {
                    worldPosition: endPoint_v3
                }
            ).
            call(() => {
                if (isLoop) {
                    this.move(true)
                }
            })
            .start()
    }
}
